define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'friends/friends/index' + location.search,
                    add_url: 'friends/friends/add',
                    edit_url: 'friends/friends/edit',
                    del_url: 'friends/friends/del',
                    multi_url: 'friends/friends/multi',
                    import_url: 'friends/friends/import',
                    table: 'friends',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'users.nickname', title: __('Users_id')},
                        {field: 'tags.title', title: __('Tags_id')},
                        // {field: 'longitude', title: __('Longitude'), operate:'BETWEEN'},
                        // {field: 'latitude', title: __('Latitude'), operate:'BETWEEN'},
                        {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'play_start_time', title: __('Play_start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'play_end_time', title: __('Play_end_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        // {field: 'content', title: __('Content'), operate: 'LIKE'},
                        {field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'users_num', title: __('Users_num')},
                        // {field: 'hot', title: __('Hot')},
                        {field: 'browse', title: __('Browse')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 启动和暂停按钮
            $(document).on("click", ".btn-start", function () {
                //在table外不可以使用添加.btn-change的方法
                //只能自己调用Table.api.multi实现
                //如果操作全部则ids可以置为空
                var ids = Table.api.selectedids(table);
                Table.api.multi("changestatus", ids.join(","), table, this);
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $("#c-type").change(function () {
                var type=$(this).val();
                $("#c-tags_id_text").data('selectPageObject').option.params = function () {
                    return {custom: {type: type}};
                };
            });

            Controller.api.bindevent();
        },
        edit: function () {
            $("#c-tags_id").data("params", function(){
                return {custom: {type: $("#c-type").val()}};
            });
            $("#c-type").change(function () {
                var type=$(this).val();
                $("#c-tags_id_text").data('selectPageObject').option.params = function () {
                    return {custom: {type: type}};
                };
            });

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
